package org.redis;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;

public class RedisCacheStorageImpl<K, V> implements RedisCacheStorage<String,V>{
    
    private static final Logger logger = LoggerFactory.getLogger(RedisCacheStorageImpl.class);
    private RedisClient redisClient;
    /** 
     * 默认过时时间 
     */  
    private static final int EXPRIE_TIME =3600*24;   

    @Override
    public boolean set(String key, V value) {
        return set(key, value, EXPRIE_TIME);
    }

    @SuppressWarnings("finally")  //TODO 这里加上这个Warnings真的好嘛
    @Override
    public boolean set(String key, V value, int exp) {
        Jedis jedis = null;
        String jKey = CacheUtils.toJsonString(key);
        String jValue = CacheUtils.toJsonString(value);
        //操作是否成功  
        boolean isSucess =true;  
        if(StringUtils.isEmpty(jKey)){
            logger.info("Key is empty");
            return false;
        }
        try {
          //获取客户端对象  
            jedis = redisClient.getResource();
            jedis.setex(jKey, exp, jValue);
        } catch (Exception e) {
            logger.info("redis server cannot releate");
            if(jedis != null){
                //释放jedis对象
                redisClient.brokenResource(jedis);
            }
            return false;
        } finally {
            if(isSucess){
                //返还连接池  
                redisClient.returnResource(jedis);
            }
            return true;
        }
    }

    

    @SuppressWarnings("finally")
    @Override
    public boolean remove(String key) {
        if(StringUtils.isEmpty(key)){
            return false;
        }
        Jedis jedis = redisClient.getResource();
        boolean isSucceed = true;
        try {
            jedis.del(key);
        } catch (Exception e) {
            logger.info("redis server cannot releate");
            //释放jedis 对象  
            isSucceed = false;
            redisClient.brokenResource(jedis);
            return false;
        } finally {
            //返还连接池  
            if(isSucceed){
                redisClient.returnResource(jedis);
            }
            return true;
        }
        
    }

    @Override
    public boolean hset(String cacheKey, String key, V value) {
        if(StringUtils.isEmpty(key)){
            logger.info("key is empy ");
            return false;
        }
        Jedis jedis = null;
        Boolean isSucceed = true;
        try {
            jedis = redisClient.getResource();
            jedis.hset(cacheKey, key, (String) value);
        } catch (Exception e) {
            logger.info("redis server cannot releate");
            isSucceed = false;
          //释放jedis 对象  
            redisClient.brokenResource(jedis);
        } finally {
            if(jedis != null){
              //返还连接池  
                redisClient.returnResource(jedis);
            }
        }
        return isSucceed;
    }

    @Override
    public V hget(String cacheKey, String key) {
        Jedis jedis = null;
        String value = null;
        if(StringUtils.isEmpty(key)){
            return null;
        }
        try{
            jedis = redisClient.getResource();
            value = jedis.hget(cacheKey, key);
          //返还连接池  
            redisClient.returnResource(jedis);
        } catch (Exception e){
            logger.info("");
          //释放jedis 对象  
            redisClient.brokenResource(jedis);
        }
        return (V) value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, V> getHash(String cacheKey) {
        if(StringUtils.isEmpty(cacheKey)){
            logger.info("cacheKey is empty!");  
            return null;
        }
        Jedis jedis = null;
        Map<String, V> map = null;
        try {
            jedis = redisClient.getResource();
            map = (Map<String, V>) jedis.hgetAll(cacheKey);
        } catch (Exception e) {
            logger.info("redis server cannot releate");
            return null;
        }
        return map;
    }

    @Override
    public V get(String key) {
        if(StringUtils.isEmpty(key)){
            logger.info("key is empty");
            return null;
        }
        String jValue = null;
        Jedis jedis = redisClient.getResource();
        try {
            jValue = jedis.get(key);
            //返回连接池
            redisClient.returnResource(jedis);
        } catch (Exception e) {
            logger.info("redis server cannot releate");
            redisClient.brokenResource(jedis);
        }
        return (V) jValue; 
    }
}
